# coding:utf-8
from os import sep, pardir

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DATE, ForeignKey  # 导入外键
from sqlalchemy.orm import relationship  # 创建关系

# path = pardir + sep + 'db' + sep + 'files' + sep


engine = create_engine("sqlite:///test.db", encoding="utf-8")

Base = declarative_base()  # 生成orm基类


class Company(Base):
    __tablename__ = "company"

    name = Column(String(20), primary_key=True)
    location = Column(String(20))

    def __repr__(self):
        return "name:{0} location:{1}".format(self.name, self.location)


class Phone(Base):
    __tablename__ = "phone"

    id = Column(Integer, primary_key=True)
    model = Column(String(32))
    price = Column(String(32))
    company_name = Column(String(32), ForeignKey("company.name"))
    company = relationship("Company", backref="phone_of_company")

    def __repr__(self):
        return "{0} model:{1},sales:{2} sales:{3} price:{4}".format(self.id, self.model, self.sales, self.price)


Base.metadata.create_all(engine)  # 创建表
